#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :page_3.py
# @Time      :2024/10/14 18:27
# @Author    :zhou_yi
import streamlit as st

from st_componets.my_st_comps import st_select_box_dic_opt
from utils.functions import rmb_upper
from st_componets.st_form import st_contract_form
from st_componets.st_fn import Fn

from st_componets.st_init import st_session_init

# st.set_page_config(page_title='功能管理', layout='wide')
st_session_init()  # 会话设置

"""  ----------------------页面-page3---------------------------   """

st.markdown('### 合同文件')

optionsA = Fn.get_company('有害垃圾', '清运')

if not optionsA:
    st.error('数据库故障...待维护')
    st.stop()
st.session_state.contract_partA = optionsA

optionsB = Fn.get_company('签订合同')

if not optionsB:
    st.error('数据库故障...待维护')
    st.stop()
st.session_state.contract_partB = optionsB
with st.sidebar:
    with st.container(border=True):
        st.subheader("参数配置")
        title = st.text_input(label='合同名称')
        st.session_state.contract_title = title

        partA = st.selectbox(label='甲方', options=[itm['name'] for itm in optionsA])
        st.session_state.current_contract_partA = partA

        partB = st.selectbox(label='乙方', options=[itm['name'] for itm in optionsB])
        st.session_state.current_contract_partB = partB

        Fn.start_date_to_end_date()  # 服务期范围选择

        service_local = st.text_input(label='服务地点', value=st.session_state.current_contract_partA)

        signal_price = st.number_input(label='单价', min_value=0.00)
        nums = st.number_input(label='垃圾桶数', min_value=1)
        price = st.number_input(label='合同金额',
                                min_value=0.00,
                                value=signal_price * nums * st.session_state.months
                                )

        # 选择生成文件的模板从路径tpl 选择 含有 【合同】 【模版】 的文件
        contract_current_tpl = st_select_box_dic_opt(label='应用模板', options=Fn.get_tpl_files(key="合同_模版"))
        st.session_state.contract_current_tpl = contract_current_tpl

with st.container(border=True):
    st.markdown(f"###### 合同名称：{title}")
    st.markdown(f"###### 合同期：{Fn.service_date()},共{st.session_state.months}个月")
    st.markdown(f"###### 合同金额：{price}")

    with st.columns(3)[0]:
        if not (title and signal_price and price):
            st.info('生成合同文件前请确保填写 合同名称/单价/总价 ')

        st.write('')
        Fn.download_contract_docx_file(tpl=st.session_state.contract_current_tpl,
                                       context=st.session_state.contract_context,
                                       title=st.session_state.contract_title,
                                       )

    st.divider()

    colA = st.columns([2, 2, 2])
    with colA[0]:
        st.markdown("###### 甲方:")
        formA = st_contract_form('partA', border=True,
                                 default_value=list(filter(lambda x: x['name'] == partA, optionsA))[0],
                                 )
    with colA[1]:
        st.markdown("###### 已方:")
        formB = st_contract_form('partB', border=True,
                                 default_value=list(filter(lambda x: x['name'] == partB, optionsB))[0])

    # 模版需要的数据
    st.session_state.contract_context = {
        'title': st.session_state.contract_title,
        'partA': formA,
        'partB': formB,
        'service_local': service_local,
        'service_date': Fn.service_date(),
        'signal_price': f"{signal_price:.2f}",
        'months': f"{st.session_state.months}",
        'nums': f"{nums}",
        'price': f"{price:.2f}",
        'up_price': rmb_upper(price)
    }
